package org.csu.tank.ui;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.scene.SubScene;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import org.csu.tank.GameConfig;
import org.csu.tank.GameType;
import org.csu.tank.TankApp;
import javafx.animation.PauseTransition;
import javafx.util.Duration;
import org.csu.tank.client.TankClient;
import org.csu.tank.controller.SucceedController;
import org.csu.tank.bean.SucceedIntegration;

import static com.almasb.fxgl.dsl.FXGL.*;
import static com.almasb.fxgl.dsl.FXGL.inc;

/**
 * 成功过关场景
 */
public class SuccessScene extends SubScene {
    private final PauseTransition pt;
    public SuccessScene(){
        // 导入设计的胜利界面
        FXMLLoader fxmlLoader = new FXMLLoader();
        Parent root = null;
        try {
            fxmlLoader.setLocation(getClass().getResource("/org.csu.tank/SucceedPane.fxml"));
            root = fxmlLoader.load();
        }catch (Exception e){
            e.printStackTrace();
        }
        SucceedController controller = fxmlLoader.getController();

        if(FXGL.getApp().getClass() == TankApp.class){  // 普通单人模式
            controller.initialize(SucceedIntegration.succeedNotInDoubleMode(1,0,0,1,1,3,FXGL.geti("level")+""));
        }else { // 多人对战模式
            TankClient tankClient = FXGL.<TankClient>getAppCast();
            controller.initialize(SucceedIntegration.succeedNotInDoubleMode(tankClient.hashCode(),0,0,0,tankClient.getEnemyKills(),5,"PVP"));
        }
        getContentRoot().getChildren().add(root);


        pt = new PauseTransition(Duration.seconds(4));
        pt.setOnFinished(event -> {
            if (geti("level") == GameConfig.MAX_LEVEL) {    // 如果已经通关
                getDialogService().showConfirmationBox("WIN! Passed all levels. Continue?", result -> {
                    if (result) {
                        getGameController().gotoMainMenu();
                    } else {
                        getGameController().exit();
                    }
                });
            } else if(geti("level") == -1){     // 当前为多人对战模式
                getDialogService().showConfirmationBox("DEAD! Do you want to try again?", result -> {
                    if (result) {
                        // Todo 如果玩家选择yes，直接开始，又一次加入游戏
                        FXGL.<TankClient>getAppCast().buildAndStartLevel();
                        FXGL.getSceneService().popSubScene();       // 关闭子场景
                    } else {
                        getGameController().gotoMainMenu();
                    }
                });
            } else {
                FXGL.getSceneService().popSubScene();
                inc("level", 1);
                FXGL.<TankApp>getAppCast().buildAndStartLevel();
            }
        });
    }

    @Override
    public void onCreate() {
        //清理关卡的残留(这里主要是清理声音残留)
        getGameWorld().getEntitiesByType(GameType.BULLET, GameType.ENEMY, GameType.PLAYER).forEach(Entity::removeFromWorld);
        pt.play();
    }
}
